package cn.web.demo.service.impl;

import cn.web.demo.mapper.RoleMapper;
import cn.web.demo.model.Role;
import cn.web.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<Role> findAllRoles() {
        return roleMapper.findAllRoles();
    }

    @Override
    public void save(Role role,String ids) {
        if(role.getId()==0){
            roleMapper.save(role);
            roleMapper.intRolePermission(role.getId(),ids);
        }else {
            roleMapper.findRoleupdate(role);
            roleMapper.findRolePermissionDelt(role.getId());
            roleMapper.intRolePermission(role.getId(),ids);
        }
    }

    @Override
    public Role FindByRoleId(Integer id) {
        return roleMapper.FindByRoleId(id);
    }

    @Override
    public String findPermissionIdsByRoleId(Integer id) {
        String ids="";
        List<Integer> list=roleMapper.findPermissionIdsByRoleId(id);
        for(Integer idc :list){
            ids+=idc+",";
        }
        if(ids.length()>0){
            ids= ids.substring(0,ids.length()-1);
        }
        return ids;
    }

    @Override
    public void findRolePermissionDelt(int id) {
        roleMapper.findRoleDelt(id);
        roleMapper.findRolePermissionDelt(id);
    }


}
